<%@page import="entity.Admin" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
Admin admin = (Admin) session.getAttribute("admin");
String deptName = "";
if ("部门".equals(admin.getIsSuper())) {
  deptName = admin.getDeptName();
}
%>

<div class="callout callout-left">
  <span class="callout-title text-muted">密钥管理</span>
</div>

<div class="callout">
  <form class="form-inline" action="" method="post">
    <div class="form-group">
      <label for="keySeg_search">密钥名称：</label>
      <input type="text" class="form-control" id="keySeg_search" name="keySeg_search" placeholder="密钥片段"
             autocomplete="off"/>
    </div>&nbsp;&nbsp;&nbsp;
    <a class="btn btn-primary" id="doSearch" href="javascript:;"><span
            class="glyphicon glyphicon-search">&nbsp;搜索</span></a>&nbsp;&nbsp;&nbsp;
  </form>
</div>

<div class="callout callout-white">
  <div class="table-responsive">
    <table class="table table-hover">
      <thead>
      <tr>
        <th>编号</th>
        <th>软件名称</th>
        <th>所属商品</th>
        <th>密钥片段</th>
        <th>激活模式</th>
        <th>激活服务器</th>
        <th>部门总分配数量</th>
        <th>用户总分配数量</th>
        <th>用户总激活数量</th>
        <th>创建时间</th>
        <th>动作</th>
      </tr>
      </thead>
      <tbody id="keys_content">
      </tbody>
    </table>
  </div>
</div>

<script>
require.config({
  baseUrl: '<%=path %>/js',
  paths: {
    jquery: 'jquery-1.9.1.min',
    common: 'common',
    bootstrapswitch: 'bootstrap-switch.min'
  }
});

require(['jquery', 'common', 'bootstrapswitch'], function ($, common) {
//console.log("商品管理模块载入");

var workspace = function () {

  var domKey;
  var dataOpt;
  var softwares;
  var deptSearch = "";

  var init = function () {
    domKey = common.commData.domKey;
    dataOpt = common.dataOpt;

    _initSoftsTables();
    _searchSoft();
  }

  var _initSoftsTables = function () {
    softwares = common.ajaxLoadData(dataOpt.softwareOpt);
    dataOpt.softwareOpt.data = {}

  var template = " <tr><td>#row#</td>" +
    "<td>#softName#</td>" +
    "<td>#category#</td>" +
    "<td>#keySeg#</td>" +
    "<td>#actMode#</td>" +
    "<td>#kmsServer#</td>" +
    // "<td>#activetotal#</td>" +
    "<td>#deptlic#</td>" +
    "<td>#userlic#</td>" +
    "<td>#usedlic#</td>" +
    "<td>#createDate#</td>" +
    "<td class='text-primary'>" +
    "<a href='#myModal' class='dept_lic' data-toggle='modal' data-softname='#softName#'><span class='glyphicon glyphicon-search'>部门分配情况</span></a>&nbsp;&nbsp;" +
    "<a href='#myModal' class='user_lic' data-toggle='modal' data-softname='#softName#'><span class='glyphicon glyphicon-search'>用户激活情况</span></a>" +
    "</td></tr>";

    var row = 1;
    var html = "";
    var thisT = "";
    for (var i = 0, n = softwares.length; i < n; i++) {
      thisT = template;
      thisT = thisT.replace(/#row#/g, row++).replace(/#softName#/g, softwares[i]["softName"]).replace(/#keySeg#/g, softwares[i]["keySeg"]).replace(/#actMode#/g, softwares[i]["actMode"])
          .replace(/#kmsServer#/g, softwares[i]["kmsServer"]).replace(/#deptlic#/g, softwares[i]["departLicense"]).replace(/#userlic#/g, softwares[i]["userLicense"])
          .replace(/#usedlic#/g, softwares[i]["usedLicense"]).replace(/#createDate#/g, softwares[i]["createDate"]).replace(/#category#/g, softwares[i]["category"]);
      html += thisT;
    }
    $("#keys_content").html(html);

    deptLicenseModal();
    userLicenseModal();
  }

  // 部门分配详情
  var deptLicenseModal = function () {
    var modalBodyTemplate = "<div class='callout callout-white'>" +
      "<div class='table-responsive'>" +
      "<table class='table table-hover'>" +
      "<thead><tr>" +
      "<th>编号</th>" +
      "<th>部门名称</th>" +
      "<th>软件名称</th>" +
      "<th>许可数量</th>" +
      "<th>总分配数量</th>" +
      "<th>总激活数量</th>" +
      "<th>动作</th>" +
      "</tr></thead>" +
      "<tbody id='modal_license'>" +
      "</tr></tbody></table></div></div>";

    var template = "<tr><td>#id#</td>" +
      "<td>#deptName#</td>" +
      "<td>#softName#</td>" +
      "<td>#licenseNum#</td>" +
      "<td>#assigned#</td>" +
      "<td>#activated#</td>" +
      "<td><a href='javascript:;' class='user_dept_lic' data-deptname='#deptName#'><span class='glyphicon glyphicon-search'></span>用户激活情况</a></td></tr>";

    // 查看该部门下的用户激活情况
    var uesrDeptLicense = function (open) {
      $(".user_dept_lic").each(function () {
        var deptName = $(this).data("deptname");

        $(this).click(function () {
          $(open).next().click();
          deptSearch = deptName;
          $("#userlic_search").click();
        });
      });
    }

    $(".dept_lic").each(function () {
      var softName = $(this).data("softname");
      dataOpt.deptLicenseOpt.data = {
        "softName": softName,
        "deptName": encodeURI(encodeURI("<%=deptName %>"))
      }
      var deptLicenses = common.ajaxLoadData(dataOpt.deptLicenseOpt);

      $(this).click(function () {
        common.cleanModal();
        domKey.modal.find(".modal-dialog").addClass("modal-lg");
        domKey.modalLabel.html("部门分配详情");
        domKey.modalBody.html(modalBodyTemplate);
        domKey.modalAct.hide();
        var thisT = "";
        var html = "";
        for (var i = 0, n = deptLicenses.length; i < n; i++) {
          thisT = template;
          thisT = thisT.replace(/#id#/g, i + 1).replace(/#deptName#/g, deptLicenses[i]["deptName"]).replace(/#softName#/g, deptLicenses[i]["softName"])
              .replace(/#licenseNum#/g, deptLicenses[i]["licenseNum"]).replace(/#assigned#/g, deptLicenses[i]["assigned"]).replace(/#activated#/g, deptLicenses[i]["activated"]);
          html += thisT;
        }
        $("#modal_license").html(html);
        uesrDeptLicense(this);
      });
    });
  }

  // 用户分配详情
  var userLicenseModal = function (deptName) {
    var modalBodyTemplate = "<div class='callout'>" +
      "<form class='form-inline'>" +
      "<div class='form-group'>" +
      "<label for='userName_search'>账号：</label>" +
      "<input type='text' class='form-control' id='userName_search' name='userName_search' placeholder='用户账号' autocomplete='off' />" +
      "</div>&nbsp;&nbsp;&nbsp;" +
      "<a class='btn btn-primary' id='userlic_search'><span class='glyphicon glyphicon-search'>&nbsp;搜索</span></a>" +
      "</form></div>" +
      "<div class='callout callout-white'>" +
      "<div class='table-responsive'>" +
      "<table class='table table-hover'>" +
      "<thead><tr>" +
      "<th>编号</th>" +
      "<th>用户账号</th>" +
      "<th>用户所在部门</th>" +
      "<th>软件名称</th>" +
      "<th>授权数量</th>" +
      "<th>已激活数量</th>" +
      "</tr></thead>" +
      "<tbody id='modal_license'>" +
      "</tr></tbody></table></div></div>";

    var template = "<tr><td>#id#</td>" +
      "<td>#userName#</td>" +
      "<td>#deptName#</td>" +
      "<td>#softName#</td>" +
      "<td>#assigned#</td>" +
      "<td>#activated#</td>";

    // 搜索用户账户
    var search = function (softName) {
      $("#userlic_search").click(function () {
        var opt = {
          "softName": softName,
          "deptName": deptSearch,
          "userName": $("#userName_search").val()
        }
        initUserLicTable(opt);
      });
    }

    // 初始化表格
    var initUserLicTable = function (opt) {
      dataOpt.userLicenseOpt.data = {
        "softName": opt["softName"],
        "deptName": encodeURI(encodeURI(opt["deptName"])),// 选择的部门
        "userName": encodeURI(encodeURI(opt["userName"]))// 搜索
      }
      var userLicenses = common.ajaxLoadData(dataOpt.userLicenseOpt);
      dataOpt.userLicenseOpt.data = {}

      var thisT = "";
      var html = "";
      for (var i = 0, n = userLicenses.length; i < n; i++) {
        thisT = template;
        thisT = thisT.replace(/#id#/g, userLicenses[i]["id"]).replace(/#deptName#/g, userLicenses[i]["deptName"])
            .replace(/#softName#/g, userLicenses[i]["softName"]).replace(/#userName#/g, userLicenses[i]["userName"])
            .replace(/#assigned#/g, userLicenses[i]["assigned"]).replace(/#activated#/g, userLicenses[i]["activated"]);
        html += thisT;
      }
      $("#modal_license").html(html);
    }

    $(".user_lic").each(function () {
      var softName = $(this).data("softname");
      var opt = {
        "deptName": "",// 每次点击清除选择的部门
        "softName": softName,// 固定软件
        "userName": ""
      }
      $(this).click(function () {
        common.cleanModal();
        domKey.modal.find(".modal-dialog").addClass("modal-lg");
        domKey.modalLabel.html("用户分配详情");
        domKey.modalBody.html(modalBodyTemplate);
        search(softName);// 固定软件
        domKey.modalAct.hide();
        initUserLicTable(opt);
      });
    });
  }

  // 搜索密钥
  var _searchSoft = function () {
    $("#doSearch").click(function () {
      dataOpt.softwareOpt.data = {
        // softName
        "keySeg": encodeURI(encodeURI($("#keySeg_search").val()))
      }
      _initSoftsTables();
    });
  }

  return {
    init: init
  }
}();

$(function () { workspace.init(); });

});
requirejs.onError = function (err) {
  console.error(err);
  if (err.requireType === 'timeout') {
    console.error('modules: ' + err.requireModules);
  }
  throw err;
}
</script>
